
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zfth/api/AlarmApi.dart';

import '../../components/form/LFormLogic.dart';
import '../../components/form/LFormPage.dart';
import '../../components/list/LCellListLogic.dart';
import '../../utils/Settings.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../utils/StoreData.dart';



class ThAlarmHandleDetailPage extends StatefulWidget{
  const ThAlarmHandleDetailPage({super.key});

  @override
  State<ThAlarmHandleDetailPage> createState() =>ThAlarmHandleDetailState();

}

class ThAlarmHandleDetailState extends State<ThAlarmHandleDetailPage> with AutomaticKeepAliveClientMixin{
  String formTag = "ThAlarmHandleDetail";
  late final LFormLogic lFormLogic;
  bool keepAlive = true;
  List formItems = [
    {"field":'deviceRegion',"label":'区域',"component":'select',"readonly":true,"required":false,"isShowLabel":true,"dictCode":Settings.dictCodes["区域"]},
    {"field":'deviceRoom',"label":'电房',"component":'select',"readonly":true,"required":false,"isShowLabel":true,"dictCode":Settings.dictCodes["配电室"]},
    {"field":'deviceNo',"label":'终端名称',"component":'input',"readonly":true,"required":false,"isShowLabel":true},
    {"field":'deviceNo',"label":'终端编码',"component":'input',"readonly":true,"required":false,"isShowLabel":true},
    {"field":'createTime',"label":'报警时间',"component":'datetime',"readonly":true,"required":false,"isShowLabel":true},
    {"field":'alarmType',"label":'报警类型',"component":'select',"readonly":true,"required":false,"isShowLabel":true,"dictCode":Settings.dictCodes["报警类型"]},
    {"field":'monitorVal',"label":'报警值',"component":'input',"readonly":true,"required":false,"isShowLabel":true},


    {"field":'alarmResultType',"label":'报警结果类型',"component":'select',"readonly":true,"required":false,"isShowLabel":true,"dictCode":Settings.dictCodes["报警结果类型"]},
    {"field":'actionType',"label":'处理类型',"component":'select',"readonly":false,"required":true,"isShowLabel":true,"dictCode":Settings.dictCodes["报警处理类型"]},
    {"field":'actionResult',"label":'处理结果',"component":'input',"readonly":false,"required":false,"isShowLabel":true},

  ];
  Map formData={};
  late String returnTag;


  @override
  void initState() {
    //获取页面传入的参数
    formData=Get.arguments["item"];
    formData["actionType"]="1";

    returnTag = Get.arguments["tag"];

    lFormLogic = Get.put(LFormLogic(),tag:formTag);
    lFormLogic.init(formItems: formItems, formData: formData,tag: formTag,onChangeCallback: onChange);

    super.initState();
  }





  @override
  void dispose() {
    Get.delete<LFormLogic>(tag: formTag);
    super.dispose();
  }

  @override
  bool get wantKeepAlive => keepAlive;
  bool isShowButton = true;

  ///内容改变后的回调
  void onChange(re){}

  void submit() async{

    if((lFormLogic.getFormKey().currentState as FormState).validate()){
      EasyLoading.show();
      dynamic res =  await AlarmApi.thAlramHandle(formData);
      if(res["success"]){
        EasyLoading.dismiss();
        EasyLoading.showSuccess(res["message"]);
        setState(() {isShowButton = false;});
        Get.back();
        //调用父页面的刷新方法
        Get.find<LCellListLogic>(tag: returnTag).onRefresh();
      }else{
        EasyLoading.dismiss();
        EasyLoading.showError(res["message"]);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: Settings.backgroundColor,
      appBar: AppBar(
        title: const Text("温湿度报警处理"),
        centerTitle: true,
        elevation: 0,
      ),
      body: SingleChildScrollView(
        child: Stack(
          children: [
            Column(
              children: [
                LFormPage(tag:formTag),
                Padding(
                    padding: const EdgeInsets.all(10),
                    child: Row(
                      children: [
                        Expanded(
                          child: Visibility(
                            visible: isShowButton,
                            child: SizedBox(
                              height: 50,
                              child: OutlinedButton(
                                style: ButtonStyle(
                                  backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
                                    return Settings.primaryColor;
                                  }),
                                  foregroundColor: MaterialStateProperty.resolveWith<Color>((states) {
                                    return Colors.white;
                                  }),
                                ),
                                onPressed:submit,
                                child: const Text('处理',style: TextStyle(fontSize: 17,)),

                              ),
                            ),
                          ),
                        )
                      ],
                    )
                )
              ],
            ),
          ],
        ),
      )




    );
  }


}